package qc.module.duty.api.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import qc.common.core.constants.QCAuthConstant;
import qc.common.core.exception.QCPromptException;
import qc.common.core.unify.QCUnifyReturnValue;
import qc.module.duty.dto.schedule.*;
import qc.module.duty.service.DutyScheduleMemberService;
import qc.module.duty.service.DutyScheduleService;
import qc.module.platform.dto.dept.DeptSimpleDto;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @projectName: qcdp
 * @package: qc.module.duty.api.controller
 * @date: 2023/8/11
 * @version: 1.0
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    private DutyScheduleService dutyScheduleService;

    @Autowired
    private void setDutyScheduleService(DutyScheduleService dutyScheduleService) {
        this.dutyScheduleService = dutyScheduleService;
    }

    private DutyScheduleMemberService dutyScheduleMemberService;

    @Autowired
    public void setDutyScheduleMemberService(DutyScheduleMemberService dutyScheduleMemberService) {
        this.dutyScheduleMemberService = dutyScheduleMemberService;
    }

    @Autowired
    HttpServletRequest request;

    @Autowired
    private RestTemplatePlatform restTemplatePlatform;

    /**
     * 查询排班信息
     *
     * @param condition: 查询条件
     * @return List<DutyGroupDto> 返回结果集
     * @author QcCheng Tech
     * @date 2023/8/11
     */
    @RequestMapping(value = "/query", method = {RequestMethod.POST, RequestMethod.GET})
    public List<DutyScheduleInfoDto> query(@RequestBody DutyScheduleQueryConditionDto condition) throws QCPromptException {
        // 调用 platform 微服务获取有效的部门/组织信息
        String projectCode = request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY);
        List<DeptSimpleDto> projectDepts = restTemplatePlatform.getProjectDeptList(projectCode);

        return dutyScheduleService.query(condition, getDeptIdAndNames(projectDepts));
    }

    /**
     * 设置班次
     *
     * @param condition:设置信息
     * @return String 成功返回null。失败返回错误信息
     * @author QcCheng Tech
     * @date 2023/8/12
     */
    @RequestMapping(value = "/shift", method = {RequestMethod.POST, RequestMethod.GET})
    public String setShift(@RequestBody DutyScheduleShiftSetDto condition) throws QCPromptException {
        return dutyScheduleService.setShift(condition);
    }

    /**
     * 批量设置班次
     *
     * @param condition:设置信息
     * @return String 成功返回null。失败返回错误信息
     * @author QcCheng Tech
     * @date 2023/8/12
     */
    @RequestMapping(value = "/batchshift", method = {RequestMethod.POST, RequestMethod.GET})
    public String batchSetShift(@RequestBody DutyScheduleBatchSetDto condition) throws QCPromptException {
        return dutyScheduleService.batchSetShift(condition);
    }

    /**
     * 删除班次
     *
     * @param id: 班次id
     * @return String 成功返回null。失败返回错误信息
     * @author QcCheng Tech
     * @date 2023/8/12
     */
    @RequestMapping(value = "/shift", method = {RequestMethod.DELETE})
    public String deleteShiftByParam(@RequestParam int id) {
        return dutyScheduleService.deleteShift(id);
    }

    /**
     * 删除班次
     *
     * @param id: 班次id
     * @return String 成功返回null。失败返回错误信息
     * @author QcCheng Tech
     * @date 2023/8/12
     */
    @RequestMapping(value = "/shift/{id}", method = {RequestMethod.DELETE})
    public String deleteShiftByPath(@PathVariable(required = true) int id) {
        return dutyScheduleService.deleteShift(id);
    }

    /**
     * 获取指定班次的值班成员列表
     *
     * @param id: 班次id
     * @return DutyTeamMemberDto 返回信息
     * @author QcCheng Tech
     * @date 2023/8/12
     */
    @RequestMapping(value = "/members", method = {RequestMethod.GET})
    public List<DutyScheduleShiftMemberDto> getTeamMembersByParam(@RequestParam int id) {
        return dutyScheduleMemberService.getTeamMembers(id);
    }

    /**
     * 获取指定班次的值班成员列表
     *
     * @param id: 班次id
     * @return DutyTeamMemberDto 返回信息
     * @author QcCheng Tech
     * @date 2023/8/12
     */
    @RequestMapping(value = "/members/{id}", method = {RequestMethod.GET})
    public List<DutyScheduleShiftMemberDto> getTeamMembersByPath(@PathVariable(required = true) int id) {
        return dutyScheduleMemberService.getTeamMembers(id);
    }

    /**
     * 设置指定班次的值班成员
     *
     * @return 成功返回null
     * @author QcCheng Tech
     * @date 2023/8/12
     */
    @RequestMapping(value = "/members", method = {RequestMethod.POST})
    public String setShiftMembers(@RequestBody DutyScheduleSetMembersDto membersDto) {
        return dutyScheduleService.setShiftMembers(membersDto);
    }

    /**
     * 修改指定班次人员，用于设置成员的排序号和是否为值班长
     *
     * @return 成功返回null
     * @author QcCheng Tech
     * @date 2023/8/12
     */
    @RequestMapping(value = "/member", method = {RequestMethod.POST})
    public String setShiftMember(@RequestBody DutyScheduleShiftMemberDto dto) {
        return dutyScheduleMemberService.setShiftMember(dto);
    }

    /**
     * 删除指定班次人员
     *
     * @return 成功返回null
     * @author QcCheng Tech
     * @date 2023/8/12
     */
    @RequestMapping(value = "/member", method = {RequestMethod.DELETE})
    public String deleteShiftMember(@RequestBody DutyScheduleDeleteMemberDto dto) {
        if (dto == null)
            return QCUnifyReturnValue.Warn("删除班次人员信息不能为空");
        if (dto.getId() < 1)
            return QCUnifyReturnValue.Warn("删除班次人员中的班次ID不能为空");
        if (dto.getMemberid() < 1)
            return QCUnifyReturnValue.Warn("删除班次人员中的人员ID不能为空");

        dutyScheduleMemberService.deleteShiftMember(dto.getId(), dto.getMemberid());

        return QCUnifyReturnValue.Success();
    }

    /**
     * 批量设置班次成员
     *
     * @param condition:设置信息
     * @return String 成功返回null。失败返回错误信息
     * @author QcCheng Tech
     * @date 2023/8/14
     */
    @RequestMapping(value = "/batchmembers", method = {RequestMethod.POST, RequestMethod.GET})
    public String batchSetShiftMembers(@RequestBody DutyScheduleBatchSetMembersDto condition) throws QCPromptException {
        return dutyScheduleService.batchSetShiftMembers(condition);
    }

    /***
     * 根据部门信息集合得到部门ID和名称对应关系
     *
     * @param depts 部门信息集合
     * @return java.util.Map<java.lang.Integer, java.lang.String>
     * @author QuCheng Tech
     * @since 2023/8/11
     */
    Map<Integer, String> getDeptIdAndNames(List<DeptSimpleDto> depts) {
        if (depts != null && depts.size() > 0x0) {
            Map<Integer, String> result = new HashMap<>();
            for (DeptSimpleDto dept : depts) {
                result.put(dept.getId(), dept.getName());
            }
            return result;
        }

        return null;
    }
}
